<template>
  <el-dialog
    :title="{ add: '添加权限', edit: '修改权限' }[mode]"
    width="600px"
    :visible="isShow"
    @close="cancelEvent"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item prop="enVisable" label="企业可见">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          inactive-value="0"
          active-value="1"
        /> </el-form-item></el-form>
    <template #footer>
      <div>
        <el-button @click="cancelEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysyPermissionPost, sysyPermissionPut } from '@/api/permission'
export default {
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: Boolean,
    default: false
  },
  data() {
    return {
      mode: 'add',
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        code: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        description: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        enVisible: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    cancelEvent() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysyPermissionPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mdoe === 'edit') {
            await sysyPermissionPut(this.form)
            this.$message.success('修改成功')
          }

          this.cancelEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
